package com.neusoft.yuejian_6.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian_6.R;
import com.neusoft.yuejian_6.javabean.Collection;
import com.neusoft.yuejian_6.model.CollectModel;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.neusoft.yuejian_6.base.Constant.PHOTO_URL;

public class ClassInfoActivity extends Activity {


    private ImageView class_pic;
    private TextView class_intro;
    private TextView class_phone,class_price,class_time;
    private TextView class_name;
    private ImageView left;
    private TextView class_address,class_teacher;
    private int class_id;
    private Button namebutton;
    private Collection collectResult;
    private ImageButton collect;
    private int user_id;
    private String coresult="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏

        setContentView(R.layout.activity_class_info);

        initView();
        initEvents();
        initData();
    }


    private void initView() {
        namebutton=(Button)findViewById(R.id.namebutton);
        collect=(ImageButton)findViewById(R.id.collect);
        class_pic = (ImageView) findViewById(R.id.class_pic);
        class_intro = (TextView) findViewById(R.id.class_intro);
        class_price= (TextView) findViewById(R.id.class_price);
      //  class_teacher= (TextView) findViewById(R.id.class_teacher);
        class_address = (TextView) findViewById(R.id.class_address);
        class_phone = (TextView) findViewById(R.id.class_phone);
        class_name = (TextView) findViewById(R.id.class_name);
        class_time= (TextView) findViewById(R.id.class_time);
        left=(ImageView)findViewById(R.id.left);
    }
    public void initEvents() {


        namebutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(ClassInfoActivity.this, ApplyActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("class_id",class_id);

                intent.putExtras(bundle);
                startActivityForResult(intent, 1);

            }
        });

        collect.setOnClickListener(new View.OnClickListener() {//收藏
            @Override
            public void onClick(View v) {
                collectClass();
            }
        });
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }




    public void initData(){
        getActivityData();
        getUserId();

        checkCollect();
    }

    private void getActivityData() {
        Bundle bundle = this.getIntent().getExtras();
        class_id = bundle.getInt("class_id");
        String classintro = bundle.getString("class_intro");
        String classname = bundle.getString("class_name");
        String classaddress = bundle.getString("class_address");
        // String classteacher = bundle.getString("class_teacher");
        String classtime = bundle.getString("class_time");
        int classphone = bundle.getInt("class_phone");
        int classprice = bundle.getInt("class_price");
        String tpic = PHOTO_URL + bundle.getString("class_pic");
        //String ttime = bundle.getString("t_time");
        //System.out.print("taddress");

        class_intro.setText(classintro);
        class_address.setText(classaddress);
        class_name.setText(classname);
        class_phone.setText(classphone + "");
        class_price.setText(classprice + "");
        class_time.setText(classtime);
        //  class_teacher.setText(classteacher);
        // t_price.setText(tprice+"");
        //System.out.print("train_intro");
        //   t_time.setText(ttime);
        Picasso.with(this).load(tpic).into(class_pic);
    }

    public void getUserId() {
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
    }

    //收藏活动

    private void collectClass() {
        CollectModel collectModel = new CollectModel();
        Call call = collectModel.collectClass(user_id, class_id);
        call.enqueue(new Callback<Collection>() {
            @Override
            public void onResponse(Call<Collection> call, Response<Collection> response) {

                if ("yes".equals(coresult)) {
                    Toast.makeText(ClassInfoActivity.this, "取消收藏成功！", Toast.LENGTH_SHORT).show();
                    collect.setImageResource(R.drawable.collect1);
                } else {
                    Toast.makeText(ClassInfoActivity.this, "收藏成功！", Toast.LENGTH_SHORT).show();
                    collect.setImageResource(R.drawable.collect);
                }
                refresh();
            }

            @Override
            public void onFailure(Call<Collection> call, Throwable t) {
                Toast.makeText(ClassInfoActivity.this, "fail", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void checkCollect() {
        CollectModel collectModel = new CollectModel();
        Call collectResultCall = collectModel.isCollected(user_id, class_id, 1);

        collectResultCall.enqueue(new Callback<Collection>() {
            @Override
            public void onResponse(Call<Collection> call, Response<Collection> response) {
                collectResult = response.body();
                String result = collectResult.getCollected();
                System.out.printf(result);
                if ("0".equals(result)) {
                    collect.setImageResource(R.drawable.collect1);
                    coresult = "no";
                } else {
                    coresult = "yes";
                    collect.setImageResource(R.drawable.collect);
                }
            }

            @Override
            public void onFailure(Call<Collection> call, Throwable t) {
                Toast.makeText(ClassInfoActivity.this, "fail", Toast.LENGTH_SHORT).show();
            }
        });

    }





    private void refresh() {
        checkCollect();
    }




    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */

}
